using NL;
using NL.Combat;
using NL.MiyaAssetBundle;
using NL.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;

namespace XGame.UI
{
    public class WinBattlePause : Window
    {
    	public Text sign_txt;
        public Button back_btn;
        public Button exit_btn;

        public Button music_btn;
        public Button sound_btn;
        public Button graphics_btn;
        public Button sfx_btn;

        public UIItemContainer EquipBag;
        public UIItemContainer SkillBag;

        public PageControl page_ctr;

        private bool loot_page_init_done = false;
        private bool summary_page_init_done = false;

        protected override void Start(){

            base.Start();

            page_ctr.WaitPageShow((page)=>
            {
                Debug.Log($"[GMUIMain.DoPageControlLoaded]:{page.gameObject.name}");
                if (page.gameObject.name == "Loot")
                {
                    InitLootPanel();
                }
                else if (page.gameObject.name == "Summary")
                {
                    InitSummaryPanel();
                }

            });

        }

        private void InitSummaryPanel()
        {
            if (summary_page_init_done)
            {
                return;
            }

            summary_page_init_done = true;

            var arr = _ONMySkillQuery?.Invoke();

            List<NLIni> _infos = new List<NLIni>();
            _infos.AddRange(arr);

            SkillBag.Set(_infos).WaitItemNew((item) =>
            {
                string id = item.INFOOf("id").ToString();
                item.name = $"Item_{id}";
                if (string.IsNullOrEmpty(id)) return;

            }).WaitItemClick(w =>
            {
                //ShowEquipIntro(w, _DoDefaultShow);
            }).Reset();

            if (music_btn != null){
                GameObject on = music_btn.transform.Find("on").gameObject;
                GameObject off = music_btn.transform.Find("off").gameObject;
                off.SetActive(false);
                music_btn?.onClick.AddListener( ()=>{ 

                    on.SetActive(!on.activeSelf);
                    off.SetActive(!off.activeSelf);

                 } );
            }
            if (sound_btn != null){
                GameObject on = sound_btn.transform.Find("on").gameObject;
                GameObject off = sound_btn.transform.Find("off").gameObject;
                off.SetActive(false);
                sound_btn?.onClick.AddListener( ()=>{ 

                    on.SetActive(!on.activeSelf);
                    off.SetActive(!off.activeSelf);

                 } );
            }
            if (graphics_btn != null){
                GameObject on = graphics_btn.transform.Find("on").gameObject;
                GameObject off = graphics_btn.transform.Find("off").gameObject;
                off.SetActive(false);
                graphics_btn?.onClick.AddListener( ()=>{ 

                    on.SetActive(!on.activeSelf);
                    off.SetActive(!off.activeSelf);

                 } );
            }
            if (sfx_btn != null){
                GameObject on = sfx_btn.transform.Find("on").gameObject;
                GameObject off = sfx_btn.transform.Find("off").gameObject;
                off.SetActive(false);
                sfx_btn?.onClick.AddListener( ()=>{ 

                    on.SetActive(!on.activeSelf);
                    off.SetActive(!off.activeSelf);

                 } );
            }

        }

        private void InitLootPanel(){

            if (loot_page_init_done)
            {
                return;
            }

            loot_page_init_done = true;

            var arr = _ONMyEquipmentQuery?.Invoke();

            List<NLIni> _infos = new List<NLIni>();
            _infos.AddRange(arr);

            Debug.Log(_infos.Count);

            EquipBag.Set(_infos).WaitItemNew((item) =>
            {
                string guid = item.INFOOf("guid").ToString();
                item.name = $"Item_{guid}";


                if (string.IsNullOrEmpty(guid)) return;

            }).WaitItemClick(w =>
            {
                //ShowEquipIntro(w, _DoDefaultShow);
            }).Reset();

        }

        protected override void OnShow()
        {
            base.OnShow();

            if (exit_btn != null)
            {
                exit_btn.onClick.AddListener(() =>
               {
                   _ONClick?.Invoke(this);
               });
            }
        }

        IniArrAction _ONMyEquipmentQuery;
        public WinBattlePause WaitMyEquipsQuery(IniArrAction _on)
        {
            _ONMyEquipmentQuery = _on;
            return this;
        }

        private IniArrAction _ONMySkillQuery;
        public WinBattlePause WaitMySkillsQuery(IniArrAction _on)
        {
            _ONMySkillQuery = _on;
            return this;
        }

        Action<Window>  _ONClick;
        public WinBattlePause WaitExitClick(Action<Window> _on)
        {
            _ONClick = _on;
            return this;
        }

    }
}